package com.holyn.guofang.view.distinguish;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.gigamole.navigationtabstrip.NavigationTabStrip;
import com.holyn.guofang.R;
import com.holyn.guofang.view.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Holyn on 2017/6/30.
 */

public class ProductClassfiyActivity extends BaseActivity {
    public static final String INTENT_KEY_SELECT_CODE = "INTENT_KEY_SELECT_CODE";
    public static final String INTENT_KEY_SELECT_CODE_TYPE = "INTENT_KEY_SELECT_CODE_TYPE";
    public static final String TYPE_TYPE_CODE = "TYPE_TYPE_CODE";
    public static final String TYPE_GROUP_CODE = "TYPE_GROUP_CODE";

    @BindView(R.id.nts_top)
    NavigationTabStrip ntsTop;
    @BindView(R.id.viewpager)
    ViewPager viewpager;

    private ViewPagerFragmentAdapter viewPagerFragmentAdapter;
    private List<Fragment> fragmentList;

    public static void startForResult(Activity activity, int requestCode){
        Intent intent = new Intent(activity, ProductClassfiyActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void startForResult(Fragment fragment, int requestCode){
        Intent intent = new Intent(fragment.getActivity(), ProductClassfiyActivity.class);
        fragment.startActivityForResult(intent, requestCode);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_product_classfiy;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);

        showToolbar("选择商品范围");

        fragmentList = new ArrayList<>();
        fragmentList.add(new ProductTypeFragment());
        fragmentList.add(new ProductGroupFragment());
        fragmentList.add(new ProductServiceFragment());

        viewPagerFragmentAdapter = new ViewPagerFragmentAdapter(getSupportFragmentManager(), fragmentList);

        viewpager.setAdapter(viewPagerFragmentAdapter);
        viewpager.setOffscreenPageLimit(2);

        ntsTop.setViewPager(viewpager, 0);
        ntsTop.setTabIndex(0, true);
    }

    private class ViewPagerFragmentAdapter extends FragmentStatePagerAdapter {
        private List<Fragment> fragmentList = new ArrayList<>();

        public ViewPagerFragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList == null ? 0 : fragmentList.size();
        }
    }
}
